﻿using LivestreamE_Commerce.EntityFrameworkCore;
using LivestreamE_Commerce.Model.Goods;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LivestreamE_Commerce.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MyGoodsController : ControllerBase
    {
        LivestreamE_CommerceDbContext db;
        public MyGoodsController(LivestreamE_CommerceDbContext _db)
        {
            db = _db;
        }
        [HttpGet, Route("GoosShow")]
        public IActionResult Show(int pageIndex = 1, int pageSize = 6)
        {
            var list = db.GoodsModel.ToList();
            var total = list.Count;
            list = list.OrderBy(x => x.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Ok(new { data = list, total = total });
        }
        [HttpPost, Route("Add")]
        public IActionResult Add(GoodsModel model)
        {
            db.GoodsModel.Add(model);
            var i = db.SaveChanges();
            if (i > 0)
            {
                return Ok(new { steta = true, msg = "添加成功" });
            }
            else
            {
                return Ok(new { steta = false, msg = "添加失败" });
            }
        }
        [HttpGet, Route("Del")]
        public IActionResult Del(string id)
        {
            var list = db.GoodsModel.ToList();
            list.Remove(list.Where(x => Convert.ToString(x.Id).Equals(id)).FirstOrDefault());
            var i = db.SaveChanges();
            if (i > 0)
            {
                return Ok(new { steta = true, msg = "删除成功" });
            }
            else
            {
                return Ok(new { steta = false, msg = "删除失败" });
            }
        }
        [HttpPost, Route("Upt")]
        public IActionResult Upt(GoodsModel model)
        {
            var i = db.Entry(model).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            if (i > 0)
            {
                return Ok(new { steta = true, msg = "修改成功" });
            }
            else
            {
                return Ok(new { steta = false, msg = "修改失败" });
            }
        }
    }
}
